﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;

namespace PIS.TechGrid.Gadgets.ChangeIP
{
    public partial class Form1 : Form
    {
        #region 属性

        MyIPConfigList myCfgList;

        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            loadMyConfigList();
        }

        private void btnLoadCurrent_Click(object sender, EventArgs e)
        {
            IPConfigList cfgs = IPHelper.GetCurrentIPConfigs();
            txtIP.Text = IPHelper.SerializeToXmlString(cfgs);
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtIP.Text.Trim()))
            {
                MessageBox.Show("配置信息不能为空");

                return;
            }

            IPConfigList cfgList = IPHelper.DeserializeFromXmlString<IPConfigList>(txtIP.Text);

            if (cfgList != null)
            {
                foreach (IPConfig cfg in cfgList.ConfigList)
                {
                    IPHelper.ApplyIPConfig(cfg);
                }

                MessageBox.Show("成功设置IP信息。");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            String cfgName = String.Empty;

            if (lsIP.SelectedItem != null)
            {
                cfgName = lsIP.SelectedItem.ToString();
            }
            else
            {
                cfgName = Microsoft.VisualBasic.Interaction.InputBox("请输入配置名！", "提示", "本地连接", -1, -1);
            }

            if (String.IsNullOrEmpty(cfgName))
            {
                saveCurrentConfigList(cfgName);
            }
        }

        private void btnSaveAs_Click(object sender, EventArgs e)
        {
            String cfgName = Microsoft.VisualBasic.Interaction.InputBox("请输入配置名！", "提示", "本地连接", -1, -1);

            saveCurrentConfigList(cfgName);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lsIP.SelectedItem != null)
            {
                if (myCfgList != null)
                {
                    myCfgList.Remove(lsIP.SelectedItem.ToString());
                    IPHelper.SaveMyConfigList(myCfgList);

                    loadMyConfigList();
                }
            }
        }

        private void lsIP_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadSelectedConfig();
        }

        private void btnLoadAuto_Click(object sender, EventArgs e)
        {
            IPHelper.EnableDHCP();
        }

        private void lsIP_DoubleClick(object sender, EventArgs e)
        {
            loadSelectedConfig();
        }

        private void loadMyConfigList()
        {
            myCfgList = IPHelper.LoadMyConfigList();

            lsIP.Items.Clear();

            if (myCfgList != null && myCfgList.IPConfigLists != null)
            {
                foreach (IPConfigList cfgList in myCfgList.IPConfigLists)
                {
                    lsIP.Items.Add(cfgList.Name ?? "no name");
                }
            }
            else
            {
                myCfgList = new MyIPConfigList();
            }

            if (lsIP.Items.Count > 0)
            {
                lsIP.SelectedIndex = 0;
            }
        }

        private void saveCurrentConfigList(string cfgName)
        {
            if (String.IsNullOrEmpty(txtIP.Text.Trim()))
            {
                MessageBox.Show("配置信息不能为空");

                return;
            }

            IPConfigList cfgList = IPHelper.DeserializeFromXmlString<IPConfigList>(txtIP.Text);

            IPConfigList orgCfgList = myCfgList.Get(cfgName);

            if (orgCfgList != null)
            {
                if (lsIP.SelectedItem != null && lsIP.SelectedItem.ToString() == cfgName)
                {
                    MsgBoxResult msgResult = Interaction.MsgBox(String.Format("已经存在名称为“{0}”的配置, 继续保存将覆盖此配置", cfgName), MsgBoxStyle.OkCancel);

                    if (msgResult != MsgBoxResult.Ok)
                    {
                        return;
                    }
                }
                else
                {
                    MsgBoxResult msgResult = Interaction.MsgBox(String.Format("已经存在名称为“{0}”的配置", cfgName));

                    if (msgResult == MsgBoxResult.Cancel)
                    {
                        return;
                    }
                    else
                    {
                        saveCurrentConfigList(cfgName);
                    }
                }

                int cfgIndex = myCfgList.IPConfigLists.IndexOf(orgCfgList);
                myCfgList.IPConfigLists.Remove(orgCfgList);

                myCfgList.IPConfigLists.Insert(cfgIndex, cfgList);
            }
            else
            {
                cfgList.Name = cfgName;
                myCfgList.IPConfigLists.Add(cfgList);
            }

            IPHelper.SaveMyConfigList(myCfgList);

            loadMyConfigList();

            lsIP.SelectedItem = cfgName;
        }

        private void loadSelectedConfig()
        {
            string cfgName = lsIP.SelectedItem.ToString();

            IPConfigList cfgList = myCfgList.Get(cfgName);

            if (cfgList != null)
            {
                txtIP.Text = IPHelper.SerializeToXmlString(cfgList);
            }
        }
    }
}
